package com.example.demo.controller;

//在controller层不要出现domain实体ebook

import com.example.demo.req.EbookQueryReq;
import com.example.demo.req.EbookSaveReq;
import com.example.demo.resp.CommonResp;
import com.example.demo.resp.EbookQueryResp;
import com.example.demo.resp.PageResp;
import com.example.demo.service.EbookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/ebook")
public class EbookController {

    @Resource
    private EbookService ebookService;


        @GetMapping("/list")
        public CommonResp list(@Valid EbookQueryReq req){
            CommonResp<PageResp<EbookQueryResp>> resp=new CommonResp<>();
            PageResp<EbookQueryResp> list=ebookService.list(req);
            resp.setContent(list);
            return resp;
        }

        /**
         * @RequestBody
         *   对应jsaon方式的post请求
         *
         *   ptrp  form表单就不用注解
         * */
    @PostMapping("/save")
    public CommonResp save(@RequestBody EbookSaveReq req){
        CommonResp resp=new CommonResp<>();
       ebookService.save(req);

        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id){
        CommonResp resp=new CommonResp<>();
        ebookService.delete(id);

        return resp;
    }

}
